// pages/coupon/use.js
const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {

        cityIndex:0,
        truckPath:'',
        truckStatus:0,
        sharePath1:'',
        sharePath2:'',
        shareStatus1:0,
        shareStatus2:0,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        var id=options.id;
        var coupon_no=options.coupon_no;
        var open=wx.getStorageSync('open');
        console.log('onLOad')
        if(coupon_no==undefined){
            //status=-1;
            this.showModals('参数错误1');
        }
        if(id==undefined){
            this.showModals('参数错误2');
            //status=-1;
        }
        this.getCity();
        this.getCoupon(id,coupon_no);
        this.setData({
            id:id,
            coupon_no:coupon_no
        })
        that.setData({
            platform:app.globalData.platform,          
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl,
            uploadUrl:app.globalData.uploadUrl,
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log('onshow')
        
      
    },
    getCoupon(id,coupon_no){
        let that=this;
        let data=this.data;
        var open=wx.getStorageSync('open');
        console.log('getCoupon');
        wx.request({        
            url: app.globalData.platUrl+'coupon/getCouponDetail',
            data:{from:1,user_id:open.user_id,id:id,coupon_no:coupon_no},
            success:res=>{
                console.log(res);
                var datas=res.data;
               
                if(res.statusCode==200){
                    that.setData({                     
                    username:datas.username,
                    userphone:datas.userphone,
                    model:datas.model,
                    modelIndex:datas.modelIndex,
                    vin:datas.vin, 
                    shareStatus2:1,        
                    sharePath2:datas.share2,
                    shareStatus1:1,
                    sharePath1:datas.share1,
                    truckPath:datas.truck,
                    truckStatus:1                 
                    })
                }
            }
            })
    },
    getCity:function(){
        let that=this;
        let data=this.data;
        var open=wx.getStorageSync('open');
        console.log('getCity');
        wx.request({        
            url: app.globalData.platUrl+'coupon/getCity',
            data:{from:1,user_id:open.user_id},
            success:res=>{
                console.log(res);
                if(res.statusCode==200){
                    let data=res.data;
                    //var city = Object.values(data.city); 
                    var city=[];
                    var citys=data.city;
                    citys.forEach(function(item,index){
                        city[item.id]=item.city;
                    });
                    that.setData({
                        citys:citys,
                        city:city,
                     //   coupon_no:coupon_no,
                        use:true
                    })
                }else{
                   wx.showLoading({
                     title: '系统错误',
                   }) 
                }
            }
            })
    },
    cityChange:function(e){
        console.log(e)
        let that=this;
        var index=e.detail.value;
        var cityIndex=this.data.cityIndex;
        var citys=this.data.citys;
        var open=wx.getStorageSync('open');
        var city=citys[index].id;
        if(index>0){
            if(index!=cityIndex){
               // this.getArea()
               wx.request({
                url: app.globalData.platUrl+'coupon/getArea',
                data:{from:1,user_id:open.user_id,city:city},
                success:res=>{
                    console.log(res);
                    if(res.statusCode==200){
                        let data=res.data;                
                        var area=[];
                        var areas=data.area;
                        console.log(areas);
                        areas.forEach(function(item,index){
                            console.log(item);
                            area[index]=item.city;
                        });
                        console.log(area);
                        that.setData({
                            areas:areas,
                            area:area,               
                       
                        })
                    }else{
                       wx.showLoading({
                         title: '系统错误',
                       }) 
                    }
                }
               })
            }
        }
        this.setData({
            cityIndex:index,
            areaIndex:0
        })
    },
    areaChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var areaIndex=this.data.areaIndex;
        var areas=this.data.areas;
        var open=wx.getStorageSync('open');
        var area=areas[index].id;
        if(index>0){
            if(index!=areaIndex){
               // this.getArea()
               wx.request({
                url: app.globalData.platUrl+'coupon/getStore',
                data:{from:1,user_id:open.user_id,area:area},
                success:res=>{
                    console.log(res);
                    if(res.statusCode==200){
                        let data=res.data; 
                       console.log(data);
                        var store=[];
                        var stores=data.store;
                        console.log(stores);
                        stores.forEach(function(item,index){
                            console.log(item);
                            store[index]=item.name;
                        });
                        console.log(area);
                        that.setData({
                            stores:stores,
                            store:store,               
                       
                        })
                    }else{
                       wx.showLoading({
                         title: '系统错误',
                       }) 
                    }
                }
               })
            }
        }
        this.setData({
            areaIndex:index,
            storeIndex:0
        })
    },
    storeChange:function(e){
        let that=this;
        var index=e.detail.value;
        var storeIndex=this.data.storeIndex;
        var stores=this.data.stores;
        var open=wx.getStorageSync('open');
        var store=stores[index].id;
        if(index>0){
            if(index!=storeIndex){
               var  model=stores[index].model;
               var arr=model.split(",");
                console.log(arr);
                this.setData({
                    model:arr
                })
            }
        }
        this.setData({
            storeIndex:index,
            modelIndex:0
        })
    },
    modelChange:function(e){
        let that=this;
        var index=e.detail.value;
        var modelIndex=this.data.modelIndex;
        var model=this.data.model;
        var models= model[index];      
            this.setData({
                modelIndex:index,
                models:models
            })
        
    },
    inputUserPhone:function(e){
        console.log(e);
        var value=e.detail.value;
        if(value){
            console.log(value);
            this.setData({
                userphone:value
            })
        }else{
            this.setData({
                userphone:undefined
            }) 
        }
       
    },
    inputUserName:function(e){
        console.log(e);
        var value=e.detail.value;
        if(value){
            console.log(value);
            this.setData({
                username:value
            })
         }else{
            this.setData({
                username:undefined
            }) 
        }
    
    },
    inputVin:function(e){
        console.log(e);
        var value=e.detail.value;
        if(value){
            console.log(value);
        this.setData({
            vin:value
        })
        }else{
            this.setData({
                vin:undefined
            }) 
        }
    
    },
    showModals:function(msg){
        wx.showModal({
            title: '系统提示',
            content:msg,
            success:function(res){
                return false;
            }
        })
    },
    toUse:function(e){
        let that = this;
        var open = wx.getStorageSync('open');
        let data=this.data;
        console.log(data);
        if(data.username==undefined){            
            this.showModals('请输入客户姓名');
        }else if(data.userphone==undefined){
            this.showModals('请输入客户电话');
        }else if(!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(data.userphone))){
          this.showModals('请输入正确的电话号码');
        }else if(!(/[A-Za-z0-9]{8}$/.test(data.vin))){
            this.showModals('请输入车架号');  
        }else if(data.truckStatus==0){
            this.showModals('请上传交车图片');
        }else if(data.shareStatus1==0){
            this.showModals('请上传交车图片朋友圈截图');
        }else if(data.shareStatus2==0){
            this.showModals('请上传转发小程序朋友圈截图');
        }else{
            var model = data.model;
            var modelIndex = data.modelIndex;
            var models = model[modelIndex];
            wx.request({
                url: app.globalData.platUrl+'coupon/editCoupon',
                data:{
                    from:1,
                    user_id:open.user_id,
                    username:data.username,
                    userphone:data.userphone,
                    model:models,
                    vin:data.vin,
                    coupon_no:data.coupon_no,
                    sharePath2:data.sharePath2,
                    sharePath1:data.sharePath1,
                    truckPath:data.truckPath,
                },
                success:res=>{
                    console.log(res);
                    var data=res.data;
                    if(data.status==200){
                        wx.showModal({
                            title: '系统提示',
                            content:data.message,
                            success:function(res){
                                //that.useClose();
                                //that.getList();
                               wx.navigateBack();
                               
                                that.setData({
                                    use:false
                                })
                            }
                        })
                    }else{
                        wx.showModal({
                            title: '系统提示',
                            content:data.message,
                            success:function(res){
                                return false;
                            }
                        })
                    }
                }
            })
        }
    },
    upload:function(e){
        console.log(e);
        var id=e.currentTarget.dataset.id;
        let that=this;
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sizeType:["compressed"],
            sourceType:["camera","album"],
            success(res) {
                var files=res.tempFiles[0];
              console.log(files.tempFilePath);
              console.log(files);
              that.uploadFile(files.tempFilePath,id)
             
            } 
        })


    },
    uploadFile(data,imageType){
        wx.showToast({
            icon:"loading",
            title: '正在上传',
          })
        let that=this;
        var open=wx.getStorageSync('open');
        var uploadUrl=app.globalData.uploadUrl;
        console.log(imageType);
        wx.uploadFile({
            filePath:data,
            name: 'file',
          url: app.globalData.platUrl+'coupon/upload',
          formData:{
            imageType:imageType,
            },
            header: { 
              'content-type':'application/x-www-form-urlencoded'       
             }, 
          success:res=>{
              console.log(res.data);
              var data=JSON.parse(res.data);
              if(imageType=='truck'){
                that.setData({
                    truckStatus:1,
                    truckPath:uploadUrl+data.path
                })
              }
              if(imageType=='share1'){
                that.setData({
                    shareStatus1:1,
                    sharePath1:uploadUrl+data.path
                })
              }
              if(imageType=='share2'){
                that.setData({
                    shareStatus2:1,
                    sharePath2:uploadUrl+data.path
                })
              }

          }
        })
    },
    goBack(){
        wx.navigateBack();
      },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})